package com.corewillsoft.fireflies.gameobjects.obstacles;

import java.util.Arrays;
import java.util.List;

import com.corewillsoft.fireflies.gamelaws.LawType;
import com.corewillsoft.fireflies.gameobjects.fireflyraces.Color;

public class Hornet extends Obstacle {

	private static final List<LawType> supportedLaws = Arrays
			.asList(LawType.UPDATE);
	private static final List<LawType> createdLaws = Arrays
			.asList(LawType.HORNET_FIREFLY);

	/**
	 * Default width of hornet
	 */
	private static final int HORNET_WIDTH = 40;

	/**
	 * Default height of hornet
	 */
	private static final int HORNET_HEIGHT = 40;

	/**
	 * Default color of hornet
	 */
	private static final Color HORNET_COLOR = new Color(0.45f, 0.25f, 0.2f);

	/**
	 * Absolute value of hornet's speed (it moves with constant speed)
	 */
	private static final float HORNET_SPEED = 5f;

	/**
	 * X-component of current speed vector
	 */
	private float speedX = 0f;

	/**
	 * Y-component of current speed vector
	 */
	private float speedY = 0f;

	public Hornet(int x, int y) {
		super(x, y);
		init();
	}

	private void init() {
		setWidth(HORNET_WIDTH);
		setHeight(HORNET_HEIGHT);
		setColor(HORNET_COLOR.getRed(), HORNET_COLOR.getGreen(), HORNET_COLOR.getBlue());
	}

	/**
	 * Calculates new position of hornet and moves it to this position
	 */
	@Override
	public void updateState() {
		updateSpeed();
		setPosition(getX() + speedX, getY() + speedY);
	}

	@Override
	public ObstacleType getType() {
		return ObstacleType.HORNET;
	}

	@Override
	public boolean isActive() {
		return true;
	}

	@Override
	protected List<LawType> getSupportedLaws() {
		return supportedLaws;
	}

	@Override
	protected List<LawType> getCreatedLaws() {
		return createdLaws;
	}

	/**
	 * Calculates new X- and Y- components of speed of the hornet,
	 * delegating this work to injected trajectory to obstacle's class,
	 */
	private void updateSpeed() {
		if (trajectory == null) {
			return;
		}

		trajectory.calculateSpeed(getX(), getY(), HORNET_SPEED);

		float speedX = trajectory.getSpeedX();
		float speedY = trajectory.getSpeedY();

		setSpeed(speedX, speedY);
	}

	public void setSpeed(float speedX, float speedY) {
		this.speedX = speedX;
		this.speedY = speedY;
	}
}
